package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.enums.content.CourseAuditEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.controller.CourseAuditController;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseBaseService courseBaseService;

    @Transactional
    public CourseTeacherDTO createOrModifyCourseTeacher(CourseTeacherDTO dto, Long companyId) {

        CourseTeacherDTO courseTeacherDTO = null;

        if (ObjectUtils.isEmpty(dto.getCourseTeacherId())) {
            courseTeacherDTO = this.createCourseTeacher(dto,companyId);
            return courseTeacherDTO;
        } else {
            courseTeacherDTO = this.modifyCourseTeacher(dto, companyId);
            return courseTeacherDTO;
        }
    }

    private CourseTeacherDTO modifyCourseTeacher(CourseTeacherDTO dto, Long companyId) {
        //判断关键数据
        if (ObjectUtils.isEmpty(dto.getCourseId())||
                ObjectUtils.isEmpty(companyId)||
                ObjectUtils.isEmpty(dto.getTeacherName())) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //判断业务数据
        CourseTeacher teacher = this.getById(dto.getCourseTeacherId());
        if (ObjectUtils.isEmpty(teacher)) {
            ExceptionCast.cast(CommonErrorCode.E_100104);
        }
        //更新操作数据
        LambdaUpdateWrapper<CourseTeacher> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CourseTeacher::getTeacherName, dto.getTeacherName());
        updateWrapper.set(CourseTeacher::getPosition, dto.getPosition());
        updateWrapper.set(CourseTeacher::getIntroduction, dto.getIntroduction());
        updateWrapper.set(CourseTeacher::getPhotograph, dto.getPhotograph());
        updateWrapper.eq(CourseTeacher::getCourseId, dto.getCourseId());
        updateWrapper.eq(CourseTeacher::getId, dto.getCourseTeacherId());
        boolean result = this.update(updateWrapper);
        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }
        //返回结果
        CourseTeacher teacherResult = this.getById(dto.getCourseTeacherId());
        CourseTeacherDTO courseTeacherDTO = CourseTeacherConvert.INSTANCE.entity2dto(teacherResult);

        return courseTeacherDTO;
    }

    private CourseTeacherDTO createCourseTeacher(CourseTeacherDTO dto,Long companyId) {
        //判断关键数据
        if (ObjectUtils.isEmpty(dto.getCourseId())||
                ObjectUtils.isEmpty(companyId)||
                ObjectUtils.isEmpty(dto.getTeacherName())) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        /**
         * 判断业务数据
         *      课程基本信息
         *          是否存在
         *          是否同机构
         *          是否已下线
         *          可添加的状态：未提交、审核未通过
         */
        CourseBase courseBase = courseBaseService.getById(dto.getCourseId());
        String auditStatus = courseBase.getAuditStatus();
        if (CourseAuditEnum.AUDIT_PASTED_STATUS.getCode().equals(auditStatus) ||
                CourseAuditEnum.AUDIT_PUBLISHED_STATUS.getCode().equals(auditStatus) ||
                CourseAuditEnum.AUDIT_COMMIT_STATUS.getCode().equals(auditStatus)) {
            ExceptionCast.cast(ContentErrorCode.E_120015);
        }
        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dto2entity(dto);
        boolean result = this.save(courseTeacher);
        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }
        CourseTeacher po = this.getById(courseTeacher.getId());
        CourseTeacherDTO courseTeacherDTO = CourseTeacherConvert.INSTANCE.entity2dto(po);

        return courseTeacherDTO;
    }

    public List<CourseTeacherDTO> queryCourseTeacherList(Long courseBaseId, Long companyId) {

        //判断关键数据
        if (ObjectUtils.isEmpty(companyId) || ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        CourseBaseDTO dto = courseBaseService.getCourseBaseById(courseBaseId, companyId);
        if (ObjectUtils.isEmpty(dto)) {
            ExceptionCast.cast(CommonErrorCode.E_100104);
        }
        //构建查询构建对象
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseBaseId);
        queryWrapper.orderByDesc(CourseTeacher::getCreateDate);
        List<CourseTeacher> teacherList = this.list(queryWrapper);

        List<CourseTeacherDTO> dtoList = new ArrayList<>();
        for (CourseTeacher courseTeacher : teacherList) {
            CourseTeacherDTO teacherDTO = CourseTeacherConvert.INSTANCE.entity2dto(courseTeacher);
            dtoList.add(teacherDTO);
        }
        return dtoList;
    }

    @Transactional
    public void removeTeacher(Long courseBaseId, Long courseTeacherId, Long companyId) {
        //判断关键数据
        if (ObjectUtils.isEmpty(companyId) ||
                ObjectUtils.isEmpty(courseBaseId) ||
                ObjectUtils.isEmpty(courseTeacherId)) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

        //判断业务数据
        CourseBaseDTO courseBaseDTO = courseBaseService.getCourseBaseById(courseBaseId, companyId);
        if (ObjectUtils.isEmpty(courseBaseDTO)) {
            ExceptionCast.cast(CommonErrorCode.E_100104);
        }

        //删除教师
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseBaseId);
        queryWrapper.eq(CourseTeacher::getId, courseTeacherId);
        boolean result = this.remove(queryWrapper);
        if (!result) {
            ExceptionCast.cast(ContentErrorCode.E_120017);
        }
    }
}
